package org.scc.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.scc.model.Group;
import org.scc.model.User;
import org.scc.service.GroupService;

public class GroupAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private Group group;
	private GroupService groupService;
	private int type;
	
	public GroupAction(){
		groupService = new GroupService();
	}
	
	public void addGroup(){
		groupService.add(group);
		responseSuccess();
	}
	
	public void deleteGroups(){
		groupService.delete(ids);
		responseSuccess();
	}
	
	public void listGroups(){
		List<Group> groups = groupService.findAll();
		responeString( getGroupsJSON(groups) );
	}
	
	private String getGroupsJSON(List<Group> groups){
		JSONArray arr = new JSONArray();
		JSONObject obj = null;
		for(Group group : groups){
			obj = new JSONObject();
			obj.put("id", group.getId());
			obj.put("name", group.getName() );
			obj.put("leader", group.getLeaderName());
			obj.put("members", getMemeberNames(group.getId()) );
			obj.put("type", group.getTypeText() );
			arr.add(obj);
		}
		return arr.toString();
	}
	
	private String getMemeberNames(int groupId){
		List<User> users = groupService.getUsers(groupId);
		
		StringBuffer sb = new StringBuffer();
		for(User user : users){
			sb.append(user.getName()).append(",");
		}
		if(sb.length() > 0){
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}

	public String initEditPage(){
		group = groupService.load(id);
		return SUCCESS;
	}
	
	public void updateGroup(){
		groupService.update(group);
		responseSuccess();
	}
	
	public void findByType(){
		List<Group> groups = groupService.findByType(type);
		responeString( getSortGroupJson(groups) );
	}
	
	private String getSortGroupJson(List<Group> groups){
		JSONArray groupArr = new JSONArray();
		JSONObject groupObj = null;
		
		JSONArray userArr = null;
		List<User> users = null;
		JSONObject userObj = null;
		for(Group group : groups){
			groupObj = new JSONObject();
			groupObj.put("id", group.getId());
			groupObj.put("name", group.getName());
			groupObj.put("leaderId", group.getLeader().getId() );
			groupObj.put("leaderName", group.getLeader().getName() );
			
			userArr = new JSONArray();
			users = groupService.getUsers(group.getId());
			for(User user : users){
				userObj = new JSONObject();
				userObj.put("id", user.getId());
				userObj.put("name", user.getName() );
				userArr.add(userObj);
			}
			groupObj.put("users", userArr);
			groupArr.add(groupObj);
		}
		return groupArr.toString();
	}
	
	/**
	 * 保存每组中的人员
	 * groupId:userId,userId;
	 */
	public void saveUserGroup(){
		String[] allGroupUsers = ids.split(";");
		int groupId = 0;
		String userIds = ","; //默认userIds如果小组中没有人员，则userIds分割的长度为0
		String[] groupUsers = null;
		for(String oneGroup : allGroupUsers){
			groupUsers = oneGroup.split(":");
			groupId = Integer.parseInt( groupUsers[0] );
			if(groupUsers.length == 2){
				userIds = groupUsers[1];
			}
			groupService.addUsers(groupId, userIds);
		}
		responseSuccess();
	}
	
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
	
}
